using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 课程管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("basic.course")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class CourseController : ControllerBase
    {
        private readonly ICourseService _courseService;

        public CourseController(ICourseService courseService)
        {
            _courseService = courseService;
        }

        /// <summary>
        /// 获取课程分页列表
        /// </summary>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>课程分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<CourseDto>>> GetPagedList([FromBody] CoursePageRequest pageRequest)
        {
            var result = await _courseService.GetPagedListAsync(pageRequest);
            return ApiResponse<PagedResult<CourseDto>>.Ok(result);
        }

        /// <summary>
        /// 获取课程详情
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>课程详情</returns>
        [HttpPost]
        public async Task<ApiResponse<CourseDto>> GetById([FromBody] IdRequest request)
        {
            var course = await _courseService.GetByIdAsync(request.Id);
            return ApiResponse<CourseDto>.Ok(course);
        }

        /// <summary>
        /// 创建课程
        /// </summary>
        /// <param name="input">创建课程请求参数</param>
        /// <returns>课程详情</returns>
        [HttpPost]
        [RequirePermission("basic.course")] // 指定所需的权限
        public async Task<ApiResponse<CourseDto>> Create([FromBody] CreateCourseDto input)
        {
            var course = await _courseService.CreateAsync(input);
            return ApiResponse<CourseDto>.Ok(course);
        }

        /// <summary>
        /// 更新课程
        /// </summary>
        /// <param name="input">更新课程请求参数</param>
        /// <returns>课程详情</returns>
        [HttpPost]
        [RequirePermission("basic.course")] // 指定所需的权限
        public async Task<ApiResponse<CourseDto>> Update([FromBody] UpdateCourseDto input)
        {
            var course = await _courseService.UpdateAsync(input);
            return ApiResponse<CourseDto>.Ok(course);
        }

        /// <summary>
        /// 删除课程
        /// </summary>
        /// <param name="request">ID请求参数</param>
        [HttpPost]
        [RequirePermission("basic.course")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _courseService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }
    }
}